package com.realdtech.wechat.task.Controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Ret;
import com.realdtech.wechat.task.Service.*;
import com.realdtech.wechat.task.model.*;
import com.realdtech.wechat.task.sms.SMS;
import com.realdtech.wechat.task.sms.SMSSender;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/18.
 */
public class IndexController extends Controller{
    public final UserJoinService join=enhance(UserJoinService.class);//加盟申请表
    public final ChannelInService channel=enhance(ChannelInService.class);//渠道入驻
    public final UserCompanyService UC=enhance(UserCompanyService.class);//注册公司表
    public final DitchService DS=enhance(DitchService.class);//热门渠道表
    public final NewsService NS=enhance(NewsService.class);//资讯表
    public final UserApplyService UA=enhance(UserApplyService.class);//快速申请

    /**
     * 控制网页跳转
     */
    public void index(){

        List<DitchCredit>  list=DS.findD_4();//信用
        List<DitchVehicle>  list_two=DS.findV_4();//车辆
        List<DitchMortgage> list_three=DS.findM_4();//房屋
        List<DitchEnterprise> list_four=DS.findE_4();//企业

        setAttr("list",list);//传进index页面数据
        setAttr("list_two",list_two);//传进index页面数据
        setAttr("list_three",list_three);//传进index页面数据
        setAttr("list_four",list_four);//传进index页面数据

        //最新公告
        List<NewsBulletin> list_bu=NS.findBU();
        setAttr("id",list_bu.get(list_bu.size()-1).getId());
        setAttr("head",list_bu.get(list_bu.size()-1).getHeadline());

        setAttr("list_B",list_bu);

        //轮播图
        List<NewsSlideshow> list_sl=NS.find_Slideshow__desc();

        //轮播图数据
        setAttr("list_sl",list_sl);

        List<NewsPartnership> list_ps=NS.find_Partnership__desc();//合作商风采

        List<NewsFinance>list_finance=NS.findF();//金融咨询

        int a=list_finance.size();//查询总共有几条数据

        //以下是金融链资讯数据
        String Headline = list_finance.get(a-6).getHeadline();//标题
        String img_on = list_finance.get(a-6).getImg();//标题
        int tHref = list_finance.get(a-6).getId();//链接

        //以下是金融链资讯数据
        setAttr("Headline",Headline);
        setAttr("img_on",img_on);
        setAttr("tHref",tHref);

        setAttr("list_finance",list_finance);


        //以下是行业资讯数据
        List<NewsBusiness>list_nb=NS.findB();

        setAttr("hyt_1",list_nb.get(list_nb.size()-8).getHeadline());//头
        setAttr("htw_1",list_nb.get(list_nb.size()-8).getContent());//内容
        setAttr("htid_1",list_nb.get(list_nb.size()-8).getId());//ID
        setAttr("img",list_nb.get(list_nb.size()-8).getImg());//图片

       setAttr("list_nb",list_nb);

        //以下是贷款攻略模块
        List<NewsStrategy>list_ns=NS.findS();

        setAttr("gl_1",list_ns.get(list_ns.size()-4).getHeadline());//头
        setAttr("content_ns",list_ns.get(list_ns.size()-4).getContent());//内容
        setAttr("img_ns",list_ns.get(list_ns.size()-4).getImg());//图片
        setAttr("id_ns",list_ns.get(list_ns.size()-4).getId());//id

        setAttr("list_ns",list_ns);

        //合作商风采
        setAttr("list_ps",list_ps);

        render("index.html");
    }

    //渠道展示
    public void productShow(){

        List<DitchCredit> list=DS.findD_4();//信用
        List<DitchVehicle> list_two=DS.findV_4();//车辆
        List<DitchMortgage>list_three=DS.findM_4();//房屋
        List<DitchEnterprise>list_four=DS.findE_4();//企业
        List<NewsBulletin>list_bu=NS.findBU();//最新公告

        List<NewsSlideshow> list_sl=NS.find_Slideshow__desc();//轮播图
        setAttr("list_sl",list_sl);//轮播图数据

//        List<NewsSlideshow> list_ps=NS.find_Slideshow__desc();//合作商风采
//        setAttr("list_ps",list_ps);//合作商风采


        setAttr("id",list_bu.get(list_bu.size()-1).getId());
        setAttr("list_B",list_bu);
        setAttr("list",list);//传进index页面数据
        setAttr("list_two",list_two);//传进index页面数据
        setAttr("list_three",list_three);//传进index页面数据
        setAttr("list_four",list_four);//传进index页面数据


        render("productShow.html");
    }

    //业务展示
    public void businessShow(){

        //贷款攻略随机循环4条数据显示在右侧
        List<NewsStrategy> list_4=NS.findS_4();
        List<NewsBulletin> list_B=NS.findBU_desc();//公告栏
        List<NewsBulletin> list_bu=NS.findBU();//最新公告

        List<NewsSlideshow> list_sl=NS.find_Slideshow__desc();//轮播图
        setAttr("list_sl",list_sl);//轮播图数据

        setAttr("id",list_bu.get(list_bu.size()-1).getId());
        setAttr("head",list_bu.get(list_bu.size()-1).getHeadline());
        setAttr("list_B",list_B);
        setAttr("list_4",list_4);


        render("businessShow.html");
    }

    //资讯中心
    public void infomationCenter(){

        List<NewsBulletin> list_bu=NS.findBU();//最新公告

        setAttr("id",list_bu.get(list_bu.size()-1).getId());
        setAttr("list_B",list_bu);
        render("infomationCenter.html");
    }

    //资讯中心 - 金融链资讯
    public void infomationCenter_finchain(){
        List<NewsFinance> list_f=NS.findF_desc();

        for(NewsFinance d:list_f){
            d.put("genre",1);
        }

        renderJson(list_f);
    }

    //资讯中心 - 行业资讯
    public void infomationCenter_industry(){
        List<NewsBusiness> list_f=NS.findB_desc();

        for(NewsBusiness d:list_f){
            d.put("genre",2);
        }

        renderJson(list_f);

    }

    //资讯中心 - 贷款攻略
    public void infomationCenter_strategy(){
        List<NewsStrategy>list_f=NS.findS_desc();

        for(NewsStrategy d:list_f){
            d.put("genre",3);
        }

        renderJson(list_f);
    }

    //资讯中心详情页面
    public void informationCenterDetail(){

        List<NewsHot> li=NS.findH_10();
        String genre=getPara("genre");//获取用户点击的类型
        String id=getPara("id");//获取当前网页的ID
        int a = Integer.parseInt(id); //转换为int类型的id

        if(genre.equals("1")){//金融资讯

            NewsFinance list=NS.findFid(a);
            NewsFinance page_small=NS.findF_paging(a,"<");//查询小于
            NewsFinance page_big=NS.findF_paging(a,">");//查询大于
            setAttr("li",list);
            setAttr("small",page_small);//查询小于
            setAttr("big",page_big);//查询大于
            setAttr("genre",genre);//当前标识
        }else if(genre.equals("2")){//行业资讯

            NewsBusiness list=NS.findBid(a);
            NewsBusiness page_small=NS.findB_paging(a,"<");//查询小于
            NewsBusiness page_big=NS.findB_paging(a,">");//查询大于
            setAttr("li",list);

            setAttr("small",page_small);//查询小于
            setAttr("big",page_big);//查询大于
            setAttr("genre",genre);//当前标识
        }else if(genre.equals("3")){//贷款攻略

            NewsStrategy list=NS.findSid(a);
            NewsStrategy page_small=NS.findS_paging(a,"<");//查询小于
            NewsStrategy page_big=NS.findS_paging(a,">");//查询大于

            setAttr("li",list);
            setAttr("small",page_small);//查询小于
            setAttr("big",page_big);//查询大于
            setAttr("genre",genre);//当前标识
        }else if(genre.equals("4")){//公告栏

            NewsBulletin list=NS.findBUfor(id);
            NewsBulletin page_small=NS.findBU_paging(a,"<");//查询小于
            NewsBulletin page_big=NS.findBU_paging(a,">");//查询大于

            setAttr("li",list);
            setAttr("small",page_small);//查询小于
            setAttr("big",page_big);//查询大于
            setAttr("genre",genre);//当前标识

        }else if(genre.equals("5")){//热点资讯

            NewsHot newsHot=NS.findHids(id);
            NewsHot page_small=NS.findH_paging(a,"<");//查询小于 也就是上篇
            NewsHot page_big=NS.findH_paging(a,">");//查询大于

            setAttr("li",newsHot);
            setAttr("small",page_small);//查询小于
            setAttr("big",page_big);//查询大于
            setAttr("genre",genre);//当前标识
        }
        setAttr("list",li);
        render("informationCenterDetail.html");
    }

    //招商加盟
    public void join(){

        List<NewsSlideshow> list_sl=NS.find_Slideshow__desc();//轮播图
        setAttr("list_sl",list_sl);//轮播图数据

        render("join.html");
    }

    //热门渠道 产品详细
    public void productDetail(){

        String page=getPara("page");
        String id=getPara("id");
        if(page.equals("1")){
            //热门渠道
            List<DitchCredit> list_4=DS.findD_4();
            setAttr("list_4",list_4);
            setAttr("page","1");

            DitchCredit list=DS.findD(id);
            setAttr("list",list);

        }else if (page.equals("2")){

            List<DitchVehicle> list_4=DS.findV_4();
            setAttr("list_4",list_4);
            setAttr("page","2");

            DitchVehicle list=DS.findV(id);
            setAttr("list",list);

        }else if (page.equals("3")){

            List<DitchMortgage> list_4=DS.findM_4();
            setAttr("list_4",list_4);
            setAttr("page","3");

            DitchMortgage list=DS.findM(id);
            setAttr("list",list);
        }else if (page.equals("4")){

            List<DitchEnterprise> list_4=DS.findE_4();
            setAttr("list_4",list_4);
            setAttr("page","4");

            DitchEnterprise list=DS.findE(id);
            setAttr("list",list);
        }
        render("productDetail.html");
    }

    // 渠道入驻
    public void channelJoin(){

        String name=getPara("name");//姓名
        String tel=getPara("tel");//电话
        String company=getPara("company");//公司名称
        String address=getPara("address");//地址
        String product=getPara("product");//产品名称

        channel.add(name,tel,company,address,product);//保存渠道入驻信息
        render("join_apply.html");
    }

    //提交加盟申请表
    public void alliance(){

        String applyRemarks=getPara("applyRemarks");//备注
        String applyTel=getPara("applyTel");        //联系电话
        String applyAddress=getPara("applyAddress");//申请地区
        String applyName=getPara("applyName");      //名字

        join.add(applyName,applyTel,applyAddress,applyRemarks); //保存申请加盟信息
        render("index.html");
    }

    //短信
    public void sendMsg(){

      /* 大象贷短信接口
      Long lastSendTime = getSessionAttr("__sms_send_time");
        Date now = new Date();
        //发送间隔最少一分钟
        if (lastSendTime == null || (now.getTime() - lastSendTime) > 60000) {

            String mobileNo = getPara("phone");
            String code = SMS.sendAuthCode(mobileNo);
            setSessionAttr("__sms_send_time", now.getTime());
            setSessionAttr("__sms_auth_code", HashKit.md5(code));
            renderJson(Ret.create("success", true));
        } else {
            renderJson(Ret.create("success", false).set("msg", "发送间隔过短"));
        }*/

        String tel = getPara("phone");
        String smsCaptcha = SMSSender.sendCaptcha(tel);
        long currentTime = new Date().getTime();
        this.setCookie(SMSSender.CAPTCHA_NAME, smsCaptcha, 300);
        this.setCookie(SMSSender.CAPTCHA_LAST_SEND_TIME_NAME, currentTime + "", 300);
        setAttr("success", true);
        renderJson();
    }

    //公司注册
    public void register(){

        String fullName=getPara("fullName");//公司全称
        String applyRemarks=getPara("name");//姓名
        String email=getPara("email");      //邮箱
        String phone=getPara("phone");      //手机号码
        String vertifyCode=getPara("vertifyCode");//验证码

        String authCode = getPara("qr");
        // 判断手机验证码是否正确
        //boolean result = SMS.validateCaptcha(this, authCode);大象贷短信接口
        boolean result = SMSSender.validateCaptcha(this, authCode);

        if(result){

            UC.add(fullName,applyRemarks,email,phone);//保存信息
            setAttr("success", true);
            renderJson();
        }else{
            setAttr("success", false);
            renderJson();
        }

    }


    //公司注册成功页面
    public void operSuccess(){

        System.out.println("注册成功");
        List<DitchCredit> list=DS.findD_4();//信用
        List<NewsBusiness>list_x=NS.findB_x(9);//随机查询9条数据
        setAttr("list",list);//传进index页面数据
        setAttr("li",list_x);
        render("operSuccess.html");
    }


    //信用贷款方法
    public void credit(){

        List<DitchCredit> list=DS.find();
        renderJson(list);
    }

    //申请贷款信息 快速申请
    public void apply(){

        String loanName= getPara("loanName");//姓名
        String loanTel=getPara("loanTel");//电话号
        String loanMoney=getPara("loanMoney");//钱
        String image_name=getPara("image_name");//所申请的渠道名字
        String Deadline=getPara("Deadline");//贷款期限
        String Product=getPara("Product");//产品类型
        String Limit=getPara("Limit");//贷款额度
        String Repayment=getPara("Repayment");//还款方式
        String Interest=getPara("Interest");//贷款利率
        UA.add(loanName,loanTel,loanMoney,image_name,Deadline,Product,Limit,Repayment,Interest);
        setAttr("success", true);
        renderJson("success", true);
    }

}
